using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using BL;
using Telerik.WinControls.UI;
using BO;

namespace AnimalFarm.Product_manage
{
    public partial class ListTreatmentProduct : Telerik.WinControls.UI.RadForm
    {
        B_Treatment_Product B_treatmentProduct = new B_Treatment_Product();
        B_DairyProduct B_dairyProduct = new B_DairyProduct();
        B_MilkTreatment B_milktreatment = new B_MilkTreatment();
        public ListTreatmentProduct()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        //load treatment product
        private void loadTreatmentProduct()
        {
            DataTable aTable = B_treatmentProduct.GetAllTreatment_Product();
            grvTreatmentProduct.DataSource = aTable;
        }

        private void ListTreatmentProduct_Load(object sender, EventArgs e)
        {
            loadTreatmentProduct();
            loadContextMenu();
        }

        //load context menu
        private void loadContextMenu()
        {
            ContextMenu menu = new ContextMenu();
            MenuItem item = new MenuItem();
            item.Text = "Delete";
            menu.MenuItems.Add(item);
            grvTreatmentProduct.ContextMenu = menu;
            item.Click += new EventHandler(item_Click);
        }

        void item_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show(this, "Are you sure", "Deleting treatment product", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (result == DialogResult.Yes)
            {
                int dairyProductID = int.Parse(grvTreatmentProduct.SelectedRows[0].Cells["DairyProductID"].Value.ToString());
                int mixTreatmentID = int.Parse(grvTreatmentProduct.SelectedRows[0].Cells["MixTreatmentID"].Value.ToString());
                bool isOk = B_treatmentProduct.RemoveOneTreatment_Product(dairyProductID, mixTreatmentID);
                if (isOk)
                {
                    MessageBox.Show(this, "Treatment product deleted successful", "Treatment product deleting", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    loadTreatmentProduct();
                }
                else
                {
                    MessageBox.Show(this, "Can not delete this Treatment product", "Treatment product deleting", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void grvTreatmentProduct_Click(object sender, EventArgs e)
        {
            try
            {
                if (grvTreatmentProduct.SelectedRows.Count > 0)
                {
                    GridViewRowInfo row = grvTreatmentProduct.SelectedRows[0];
                    int dairyProduct = int.Parse(row.Cells["DairyProductID"].Value.ToString());
                    int milkTreatment = int.Parse(row.Cells["MixTreatmentID"].Value.ToString());
                    int quanlity = int.Parse(row.Cells["Quanlity"].Value.ToString());

                    txtQuanlity.Text = quanlity + "";
                    loadDairyProduct(dairyProduct);
                    loadMilkTreatment(milkTreatment);
                }
            }
            catch (Exception ex) { }
        }

        //load dairy product
        private void loadDairyProduct(int dairyProductID)
        {
            DataTable aTable = B_dairyProduct.GetAllDairyProduct();
            drdDairyProduct.DataSource = aTable;
            drdDairyProduct.DisplayMember = "DairyProductName";
            drdDairyProduct.ValueMember = "DairyProductID";
            drdDairyProduct.SelectedValue = dairyProductID;
        }
        //load milk treatment
        private void loadMilkTreatment(int milkTreatmentID)
        {
            DataTable aTable = B_milktreatment.GetAllMilkTreatment();
            drdMilkTreatment.DataSource = aTable;
            drdMilkTreatment.DisplayMember = "Description";
            drdMilkTreatment.ValueMember = "MilkTreatmentID";
            drdMilkTreatment.SelectedValue = milkTreatmentID;
        }

        private void btnSaveChange_Click(object sender, EventArgs e)
        {
            try
            {
                Treatment_Product_Info info = new Treatment_Product_Info();
                info.DairyProductID = int.Parse(drdDairyProduct.SelectedValue.ToString());
                info.MixTreatmentID = int.Parse(drdMilkTreatment.SelectedValue.ToString());
                try
                {
                    info.Quanlity = int.Parse(txtQuanlity.Text);
                }
                catch (Exception ex) { info.Quanlity = 0; }

                bool isOk = B_treatmentProduct.EditOneTreatment_Product(info);
                if (isOk)
                {
                    MessageBox.Show(this, "Treatment product updated successful", "Treatment product updating", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    loadTreatmentProduct();
                }
                else
                {
                    MessageBox.Show(this, "Treatment product updating is fail", "Treatment product updating", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex) { }
        }

    }
}
